package com.ufox.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ufox.entity.*;
import com.ufox.service.IGoodsService;
import com.ufox.utils.PicUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 商品信息表 前端控制器
 * </p>
 *
 * @author 优狐科技-www.hbufox.com
 * @since 2022-05-31
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private IGoodsService goodsService;

    @Value("${goods.goodsImage}")
    private String goodsImage;

    @Value("${picUrl}")
    private String picUrl;


    //上传图书封面
    @PostMapping("/shangchuan")
    public Result shangchuan(MultipartFile touxiang) throws IOException {
        String picUrl = PicUtil.getPicUrl(touxiang, goodsImage, this.picUrl);
        return Result.ok(picUrl);
    }

    @GetMapping("/{id}")
    protected Result selectById(@PathVariable Integer id) {
        Goods goods = goodsService.findById(id);
        return Result.ok(goods);
    }


    //分页 查询
    @PostMapping("/{page}/{size}")
    public Result findAllCond(@PathVariable int page, @PathVariable int size,@RequestBody GoodsCond goodsCond) {
        Page<Goods> pageInfo = new Page<>(page, size);
        Page<Goods> list = goodsService.selectAllByCond(pageInfo, goodsCond);
        return Result.ok(list);
    }

    //添加  或 修改
    @PostMapping
    public Result saveOrUpdateBook(@RequestBody Goods goods) {
        boolean b = goodsService.saveOrUpdate(goods);
        return  (b ? Result.ok() : Result.error(""));
    }

    //删除
    @DeleteMapping("/{id}")
    public Result delMember(@PathVariable Integer id) {
        boolean b = goodsService.removeById(id);
        return  (b ? Result.ok() : Result.error(""));
    }

    //批量删除

    @PostMapping("/batch")
    public Result delMembersBatch(@RequestBody List<Integer> ids) {
        boolean b = goodsService.removeByIds(ids);
        return  (b ? Result.ok() : Result.error(""));
    }

}

